package com.springsource.a09Proxy.jdk;

import java.lang.reflect.Proxy;

public class A09JdkProxy {

    interface ITargetService {
        void doSomething();
    }

    static class TargetService implements ITargetService {
        public void doSomething() {
            System.out.println("TargetService.doSomething");
        }
    }

    public static void main(String[] args) {
        // 用于来加载在运行期间，动态生成的字节码
        ClassLoader classLoader = ITargetService.class.getClassLoader();
        ITargetService targetService = (ITargetService) Proxy.newProxyInstance(classLoader, new Class<?>[]{ITargetService.class}, (proxy, method, args1) -> {
            System.out.println("before invoke");
            Object result = method.invoke(new TargetService(), args1);
            System.out.println("after invoke");
            return result;
        });
        targetService.doSomething();
    }

}
